package com.hex.ds.hdrs.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.hex.ds.hdrs.common.constant.ErrorCode;
import com.hex.ds.hdrs.common.exception.BizException;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class InvokeUtil {

    /**
     * POST请求
     * @param url
     * @param paramData
     * @return
     */
    public static  <T> T invoke(String url, Object paramData, Class<T> clazz) {
        String retval = invoke(url, paramData);
        return JSONObject.parseObject(retval,clazz);
    }

    /**
     * POST请求
     * @param url
     * @param paramData
     * @return
     */
    public static String invoke(String url, Object paramData) {
        String retVal = HttpUtils.httpJsonPost(url, paramData);
        if (StringUtils.isEmpty(retVal)) {
            throw new BizException(ErrorCode.ERROR_300001.getCode(), ErrorCode.ERROR_300001.getMessage(), null);
        }
        return retVal;
    }

    /**
     * GET请求
     * @param url
     * @param paramData
     * @return
     */
    public static String invoke(String url,  Map<String, Object> paramData) {
        String retVal = HttpUtils.httpJsonGet(url, paramData);
        if (StringUtils.isEmpty(retVal)) {
            throw new BizException(ErrorCode.ERROR_300001.getCode(), ErrorCode.ERROR_300001.getMessage(), null);
        }
        return retVal;
    }

    /**
     * GET请求
     * @param url
     * @param paramData
     * @param clazz
     * @param <T>
     * @return
     */
    public static  <T> T invoke(String url, Map<String, Object> paramData, Class<T> clazz) {
        String retval = invoke(url, paramData);
        return JSONObject.parseObject(retval,clazz);
    }
}
